/**
 * Created by zhang on 2016/12/14.
 */
var roleManage = {
    events:{
        "#roleManageBody .add-role click" : "addRole",
        "#roleManageBody .delete-role click" : "deleteRole"
    },
    init: function () {
        var _this = this;
        document.title = "角色管理 - 系统管理";
        var temp = Util.template($("#roleManage-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        _this.initTable();
        _this.zTreeLists();
    },
    initTable: function () {
        var _this = this;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getRoleList(),
            showLoading : false,
            async:false,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    $.each(data.result, function (i, n) {
                        n.choseO = "<div class='property1' name='workday'><div class='user-check'></div></div>";
                    });
                    $(".role-list").cakeTable({
                        head: [   
                            {field: "choseO",name: "",type:2},
                            {field: "name",name: "角色"}
                        ],
                        rowData:[
                            {key:"id",attrName:"id"}
                        ],
                        columns:data.result,
                        sort: false,
                        showPage:false,
                        showNum : false,
                        align:"center",
                        pageType:"out",
                        done:function($table){
                            $table.find("tr td:eq(0)").css("width","74px");
                            $table.find("tbody>tr").click(function (e) {
                                if (!$(this).hasClass("choice")) {
                                    var roleId = $(this).attr("id");
                                    $table.find("tbody>tr").removeClass("choice");
                                    $table.find(".property1 .user-check").removeClass("checked");
                                    $(this).addClass("choice");
                                    $(this).find(".property1 .user-check").addClass("checked");
                                    _this.getMenu(roleId);
                                }
                            })
                        }
                    })
                }   
            }
        });
    },
    getMenu : function(roleId){
        var _this = this;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getMenuOne(),
            data:{
                roleId : roleId
            },
            showLoading : false,
            async:false,
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    _this.treeObjA.checkAllNodes(false);//取消当前部门树所有的选中状态
                    var ids = data.result;
                    for(var i=0 ; i < ids.length; i ++ ) {
                        var node = _this.treeObjA.getNodeByParam("id",ids[i]);
                        _this.treeObjA.checkNode(node, true, false);
                    }
                }   
            }
        });
    },
    roleOperate : function(url,para,okText,fn){
        Util.ajaxRequest({
            type: "get",
            url: url,
            dataType: "json",
            timeout:60000,
            data:para,
            success : function(data){
                if(data.code == 200){
                    pop.alert(okText,function(){
                        $.isFunction(fn)?fn():"";
                    })
                }
            }
        })
    },
    addRole: function(){
        var _this = this;
        pop.dialog({
            popTitle: "添加角色",
            popContent: Util.template($("#addRole-template").html(), {}),
            width :　"500px",
            onLoad: function () {}
        },function(){
            var roleName = $("#roleName").val(),
                url = interfaceURL.saveRole(),
                okText ="添加成功！";
            if (!Util.isEmpty(roleName)) {
                _this.roleOperate(url,{name:roleName},okText,function(){
                    _this.initTable();
                });
            }else{
                pop.alert("请填写角色名！");
            }
        })
    },
    deleteRole: function(){
        var _this = this;
        if ($(".role-list tr.choice").length > 0 ) {
            var id = $(".role-list tr.choice").attr("id"),
                url = interfaceURL.deleteRole(),
                okText = "删除成功！";
            pop.confirm("确定删除该角色吗？",function(){
                _this.roleOperate(url,{nid:id},okText,function(){
                    _this.initTable();
                });
            })
        }else{
            pop.alert("请选择要删除的角色！")
        }
    },
    zTreeLists : function(){
        var _this = this;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getMenuList(),
            dataType: "json",
            timeout:60000,
            success : function(data){
                if(data.code == 200){
                    var zTreeObj,
                        setting = {
                            check: {
                                enable: true,
                                chkboxType: { "Y": "ps", "N": "ps" }
                            },
                            data: {
                                simpleData: {
                                    enable: true
                                }
                            },
                            callback: {
                                beforeCheck: _this.zTreeBeforeCheck,
                                onCheck: _this.zTreeOnCheck
                            }
                        },
                        treeNode = data.result;
                    zTreeObj = $.fn.zTree.init($("#accessList"), setting, treeNode);
                    var treeObj = $.fn.zTree.getZTreeObj("accessList");
                    _this.treeObjA = treeObj;
                    /*默认展开*/
                    /*treeObj.expandAll(true);*/
                }
            }
        })
    },
    zTreeBeforeCheck : function(treeId, treeNode) {
        if ($(".role-list tr.choice").length < 1) {
            pop.alert("请选择角色后再配置!");
            return false
        }
    },
    zTreeOnCheck : function(event, treeId, treeNode) {
        var _this = this,
            nodeIds ="",
            node =  $.fn.zTree.getZTreeObj("accessList").getCheckedNodes(true),
            roleId = $(".role-list tr.choice").attr("id"),
            menuId = treeNode.id,
            isAdd = treeNode.checked;
        for(var i = 0;i < node.length;i++){
            nodeIds =  nodeIds == "" ? node[i].id : nodeIds + "," + node[i].id;
        }
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.updateMenu(),
            dataType: "json",
            data:{
                roleId : roleId,
                menuIds : nodeIds
            },
            timeout:60000,
            success : function(data){
                if(data.code == 200){}
            }
        })
    } 
};